<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChWord;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;
use function Hyperf\Collection\data_get;

class ChWordMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChWord::class;
    }

    public function handleSearch(Builder $query, array $params): Builder
    {
        $query->where('main_uid',$params['main_uid']);

        $type = data_get($params,'type',0);
        if($type == 1){
            $query->where('cs_id',$params['uid']);
        }

        if(isset($params['group_id']) && filled($params['group_id'])){
            $query->where('group_id',$params['group_id']);
        }

        $query->where('is_personal',$type);

        if(!empty($params['keyword'])){
            $query->where('title','like','%'.$params['keyword'].'%')
                ->orWhere('keyword','like','%'.$params['keyword'].'%');
        }

        return $query;
    }

    public function handleOrder(Builder $query, ?array &$params = null): Builder
    {
        return $query->orderBy('sort','desc');
    }

}